package araxy.blog.configuration;

import org.eclipse.jgit.transport.ChainingCredentialsProvider;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JGitConfiguration {
    @Value("${storage.git.repo.url}")
    public String url;

    @Value("${storage.git.cert.account}")
    public String account;

    @Value("${storage.git.cert.password}")
    public String password;

    @Value("${storage.git.cert.token}")
    public String token;

    @Bean
    public CredentialsProvider credentialsProvider() {
        CredentialsProvider credentialsProvider;
        if ( !(token == null || "".equals(token)) ) {
            String tokenAccount;
            if (url.contains("github")) {
                tokenAccount = "PRIVATE-TOKEN";
            } else if ( url.contains("gitee") ) {
                tokenAccount = account;
            } else {
                tokenAccount = "";
            }
            credentialsProvider = new UsernamePasswordCredentialsProvider(tokenAccount,token);
        } else if( (!(account == null || "".equals(account)) ) && !(password == null || "".equals(password)) ) {
            credentialsProvider = new UsernamePasswordCredentialsProvider(account,password);
        } else {
            credentialsProvider = null;
        }
        return credentialsProvider;
    }

}
